import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'dart:ui' as ui;

class MineController extends GetxController {
  MineController();

  final _list = <String>['1', '2', '3', '4', '5', '6', '7', '8', '9'].obs;

  final items = <Widget>[].obs;
  var sliderValue = 0.0.obs;
  var blurValue = 0.0.obs;

  final scrollController = ScrollController();
  final gridViewKey = GlobalKey();

  late ui.Image fillImage;
  bool isImageLoaded = false;

  late Image bufImage;

  // final state = MineState();

  _initData() async {
    for (var i = 0; i < _list.length; i++) {
      Widget widget = Container(
        key: ValueKey(_list[i]),
        color: Colors.blue,
        width: (1.sw - 34.r) / 2.0,
        height: i.isEven ? 200 : 100,
        child: Center(
          child: Text(_list[i]),
        ),
      );
      items.add(widget);
    }

    final ByteData data = await rootBundle.load('assets/images/main_bg.png');
    fillImage = await loadImage(Uint8List.view(data.buffer));
    // bufImage = await blurAsset('assets/images/main_bg.png');
    update(["mine"]);
  }

  // Future<Image> blurAsset(String assetName) async {
  //   ImageProvider provider = ExactAssetImage(assetName);

  //   // Rain dance to get RGBA pixels from image
  //   final ImageStream stream = provider.resolve(ImageConfiguration.empty);
  //   final completer = Completer<ui.Image>();
  //   late ImageStreamListener listener;
  //   listener = ImageStreamListener((frame, _) {
  //     stream.removeListener(listener);
  //     completer.complete(frame.image);
  //   }, onError: (error, stack) {
  //     stream.removeListener(listener);
  //     completer.completeError(error, stack);
  //   });
  //   stream.addListener(listener);
  //   ui.Image image = await completer.future;
  //   ByteData rgbaData =
  //       (await image.toByteData(format: ui.ImageByteFormat.rawRgba))!;

  //   // These are the pixels we needed
  //   Uint32List rgbaPixels = rgbaData.buffer.asUint32List();

  //   // Now we can blur the image buffer
  //   stackBlurRgba(rgbaPixels, image.width, image.height, 42);

  //   // We use a third-party 'bitmap' library to turn the buffer into a widget
  //   final bitmap = Bitmap.fromHeadless(
  //       image.width, image.height, rgbaPixels.buffer.asUint8List());
  //   return Image.memory(bitmap.buildHeaded());
  // }

  Future<ui.Image> loadImage(Uint8List img) async {
    final Completer<ui.Image> completer = Completer();
    ui.decodeImageFromList(img, (ui.Image img) {
      isImageLoaded = true;

      return completer.complete(img);
    });
    return completer.future;
  }

  void onTap() {}

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }

  /// 在 [onDelete] 方法之前调用。
  // @override
  // void onClose() {
  //   super.onClose();
  // }

  /// dispose 释放内存
  // @override
  // void dispose() {
  //   super.dispose();
  // }
}
